package com.kehongborui.fliggy.service.impl;

import com.kehongborui.fliggy.entity.PassStation;
import com.kehongborui.fliggy.entity.Station;
import com.kehongborui.fliggy.entity.Vehicle;
import com.kehongborui.fliggy.exception.FliggyException;
import com.kehongborui.fliggy.mapper.PassStationMapper;
import com.kehongborui.fliggy.mapper.StationMapper;
import com.kehongborui.fliggy.mapper.VehicleMapper;
import com.kehongborui.fliggy.model.PassStationModel;
import com.kehongborui.fliggy.service.PassStationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.kehongborui.fliggy.enums.ExceptionMessage.PASS_STATION_ID_IS_NOT_EXIST;
import static com.kehongborui.fliggy.enums.ExceptionMessage.STATION_IS_NOT_EXIST;
import static com.kehongborui.fliggy.enums.ExceptionMessage.VEHICLE_NOT_EXIST;
import static com.kehongborui.fliggy.util.CommonUtils.getStopTime;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-22
 */
@Service
public class PassStationServiceImpl implements PassStationService {
    @Autowired
    private PassStationMapper passStationMapper;
    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public boolean updatePassStations(List<PassStationModel> passStationList) {
        passStationList.forEach(station -> {
            // 判断经停站对应列车id和经停站的id是否为空
            if (station.getVehicleId() == null || station.getId() == null) {
                Station retStation = stationMapper.selectByPrimaryKey(station.getStationId());
                throw new FliggyException(String.format(PASS_STATION_ID_IS_NOT_EXIST.getDesc(), retStation.getName()));
            }

            // 判断当前数据是否存在于数据库
            PassStation retStation = passStationMapper.selectByPrimaryKey(station.getId());
            if (retStation == null) {
                throw new FliggyException(String.format(STATION_IS_NOT_EXIST.getDesc(), station.getId()));
            }

            PassStation passStation = new PassStation();
            BeanUtils.copyProperties(station, passStation);
            passStation.setUpdateTime(new Date());

            passStationMapper.updateByPrimaryKey(passStation);
        });

        return true;
    }

    @Override
    public List<PassStationModel> findPassStationByVehicleNo(String vehicleNo) {
        // 判斷vehicleNo對應的車輛信息是否存在
        Vehicle vehicle = vehicleMapper.findByVehicleNo(vehicleNo);
        if (vehicle == null) {
            throw new FliggyException(String.format(VEHICLE_NOT_EXIST.getDesc(), vehicleNo));
        }

        List<PassStation> passStationList = passStationMapper.findPassStationByVehicleNo(vehicleNo);
        List<PassStationModel> modelList = passStationList.stream().map(s -> {
            PassStationModel model = new PassStationModel();
            BeanUtils.copyProperties(s, model);
            model.setVehicleId(s.getVehicle().getId());
            model.setVehicleName(s.getVehicle().getVehicleNo());
            model.setStationName(s.getStation().getName());
            // 设置经停站停留时间
            if (model.getSeq() == 1) {  // 设置起点站时间格式
                model.setStopTime(0);
                model.setArrTime("--:--");
            } else if (model.getSeq() == passStationList.size()) { // 设置终点站时间格式
                model.setStopTime(0);
                model.setDepTime("--:--");
            } else {
                model.setStopTime(getStopTime(model.getDepTime(), model.getArrTime()));
            }

            return model;
        }).collect(Collectors.toList());

        return modelList;
    }
}
